// EnumPostNummer.cpp : Implementation of CEnumPostNummer
#include "stdafx.h"
#include "EBASdll.h"
#include "EnumPostNummer.h"
#include "PostNummer.h"

/////////////////////////////////////////////////////////////////////////////
// CEnumPostNummer

STDMETHODIMP CEnumPostNummer::InterfaceSupportsErrorInfo(REFIID riid)
{
	static const IID* arr[] = 
	{
		&IID_IEnumPostNummer
	};
	for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		if (InlineIsEqualGUID(*arr[i],riid))
			return S_OK;
	}
	return S_FALSE;
}

CEnumPostNummer::CEnumPostNummer()
{
	m_pUnkMarshaler = NULL;
	m_records = NULL;
	m_first = TRUE;
	m_pos = 0;
	m_length = 0;
}

CEnumPostNummer::~CEnumPostNummer()
{
	if (m_records)
		m_records->Close();
}

STDMETHODIMP CEnumPostNummer::Next(/*[out]*/ IUnknown ** ppUnk, /*[out]*/ ULONG * pcFetched)
{
	_CommandPtr cmd;
	bstr_t guid, nr, by, antal;
	
	const char sql[255] = "EBAS_SP_HENT_POST_NR";

	try {
		if (!ppUnk)
			return E_POINTER;

		ULONG cFetched = 0;

		if (m_first) {
			m_first = FALSE;

			cmd.CreateInstance(__uuidof(Command));
			cmd->ActiveConnection = CDatabase::GetConnection();
			cmd->CommandText = sql;
			cmd->CommandType = adCmdStoredProc;
			cmd->Parameters->Append( cmd->CreateParameter("session", adVarChar, adParamInput, 255, gpSessionGuid) );

			m_records = cmd->Execute(NULL, NULL, adCmdStoredProc);	
			if (m_records->ADO_EOF)
				return E_ACCESSDENIED;
		}			

		if (m_records->ADO_EOF)
			return S_OK;

		guid = m_records->Fields->GetItem("GUID")->Value;
		nr = m_records->Fields->GetItem("POST_NR")->Value;
		by = m_records->Fields->GetItem("BY")->Value;

		m_length = (long)m_records->Fields->GetItem("ANTAL")->Value;

		CComObject<CPostNummer> * pInstance = NULL;
		CComObject<CPostNummer>::CreateInstance(&pInstance);
		pInstance->put_GUID(guid);
		pInstance->put_PostNr(nr);
		pInstance->put_By(by);

		pInstance->QueryInterface(ppUnk);
		m_pos += 1;
		cFetched = m_length;

		if (pcFetched)
			*pcFetched = cFetched;

		m_records->MoveNext();

	} catch (_com_error &e) {
		char err[512];
		strncpy(err, e.ErrorMessage(), 512);
		strncpy(err, " ", 1);
		strncpy(err, e.Description(), _tcslen(e.Description()));
		return e.Error();
	}

	return TRUE;
}

STDMETHODIMP CEnumPostNummer::Skip(ULONG cItems)
{
	try {
		if (m_records) {
			m_pos += cItems;
			m_records->Move(m_pos);
		}
	} catch (_com_error &e) {
		return e.Error();
	}

	return S_OK;
}

STDMETHODIMP CEnumPostNummer::Reset()
{
	_CommandPtr cmd;

	const char sql[255] = "EBAS_SP_HENT_POST_NR";

	try {
		cmd.CreateInstance(__uuidof(Command));
		cmd->ActiveConnection = CDatabase::GetConnection();
		cmd->CommandText = sql;
		cmd->CommandType = adCmdStoredProc;
		cmd->Parameters->Append( cmd->CreateParameter("session", adVarChar, adParamInput, 255, gpSessionGuid) );

		m_records = cmd->Execute(NULL, NULL, adCmdStoredProc);
		if (m_records->ADO_EOF)
			return E_ACCESSDENIED;	
	} catch (_com_error &e) {
		return e.Error();
	}

	return S_OK;
}

STDMETHODIMP CEnumPostNummer::Clone(IEnumeration ** ppEnumeration)
{
	return E_NOTIMPL;
}

STDMETHODIMP CEnumPostNummer::findBy(BSTR postNr, BSTR * pBy)
{
	_RecordsetPtr recordset;
	_CommandPtr cmd;
	bstr_t guid, nr, by, antal;
	
	const char sql[255] = "EBAS_SP_HENT_POST_NR";

	try {

		cmd.CreateInstance(__uuidof(Command));
		cmd->ActiveConnection = CDatabase::GetConnection();
		cmd->CommandText = sql;
		cmd->CommandType = adCmdStoredProc;
		cmd->Parameters->Append( cmd->CreateParameter("session", adVarChar, adParamInput, 255, gpSessionGuid) );
		cmd->Parameters->Append( cmd->CreateParameter("postNr", adVarChar, adParamInput, 255, postNr) );

		recordset = cmd->Execute(NULL, NULL, adCmdStoredProc);	
		if (recordset->ADO_EOF)
			return E_ACCESSDENIED;

		guid = recordset->Fields->GetItem("GUID")->Value;
		nr = recordset->Fields->GetItem("POST_NR")->Value;
		by = recordset->Fields->GetItem("BY")->Value;
		m_length = (long)recordset->Fields->GetItem("ANTAL")->Value;

		if (pBy)
			*pBy = by.copy();

		recordset->Close();
	} catch (_com_error &e) {
		char err[512];
		strncpy(err, e.ErrorMessage(), 512);
		strncpy(err, " ", 1);
		strncpy(err, e.Description(), _tcslen(e.Description()));
		return e.Error();
	}

	return TRUE;
}

STDMETHODIMP CEnumPostNummer::findPostNr(BSTR by, BSTR * pPostNr)
{
	_RecordsetPtr recordset;
	_CommandPtr cmd;
	bstr_t guid, nr, byen, antal;
	
	const char sql[255] = "EBAS_SP_HENT_POST_NR";

	try {

		cmd.CreateInstance(__uuidof(Command));
		cmd->ActiveConnection = CDatabase::GetConnection();
		cmd->CommandText = sql;
		cmd->CommandType = adCmdStoredProc;
		cmd->Parameters->Append( cmd->CreateParameter("session", adVarChar, adParamInput, 255, gpSessionGuid) );
		cmd->Parameters->Append( cmd->CreateParameter("postNr", adVarChar, adParamInput, 255, "%") );
		cmd->Parameters->Append( cmd->CreateParameter("by", adVarChar, adParamInput, 255, by) );

		recordset = cmd->Execute(NULL, NULL, adCmdStoredProc);	
		if (recordset->ADO_EOF)
			return E_ACCESSDENIED;

		guid = recordset->Fields->GetItem("GUID")->Value;
		nr = recordset->Fields->GetItem("POST_NR")->Value;
		byen = recordset->Fields->GetItem("BY")->Value;
		m_length = (long)recordset->Fields->GetItem("ANTAL")->Value;

		if (pPostNr)
			*pPostNr = nr.copy();

		recordset->Close();
	} catch (_com_error &e) {
		char err[512];
		strncpy(err, e.ErrorMessage(), 512);
		strncpy(err, " ", 1);
		strncpy(err, e.Description(), _tcslen(e.Description()));
		return e.Error();
	}

	return TRUE;	
}
